package me.jessyan.mvparms.demo.mvp.presenter.auth;

import android.app.Application;
import android.content.Intent;
import android.util.Log;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.mvparms.demo.utils.AvoidOnResult;
import me.jessyan.mvparms.demo.utils.MyMultiImageSelector;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import me.jessyan.mvparms.demo.mvp.contract.auth.AuthContract;
import me.nereo.multi_image_selector.utils.FileUtils;


import static android.app.Activity.RESULT_OK;


@ActivityScope
public class AuthPresenter extends BasePresenter<AuthContract.Model, AuthContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public AuthPresenter(AuthContract.Model model, AuthContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 选择图片
     */
    public  void choicePic(boolean isFront){


        //请求外部存储权限用于适配android6.0的权限管理机制
        PermissionUtil.externalStorage(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                //request permission success, do something.
                MyMultiImageSelector selector = MyMultiImageSelector.create(mRootView.getActivity());
                selector.showCamera(false);
                selector.count(1);
                selector.single();
//        selector.origin(mSelectPath);   默认选择
                selector.start(mRootView.getActivity(), new AvoidOnResult.Callback() {

                    @Override
                    public void onActivityResult(int resultCode, Intent data) {
                        if (resultCode == RESULT_OK) {
                            ArrayList<String> mSelectPath = data.getStringArrayListExtra(MyMultiImageSelector.EXTRA_RESULT);


                            mRootView.showLoading();



//                            Glide.with(getActivity()).load("file://" + path)
//                                    .into(headIv);
                            String path = mSelectPath.get(0);
                            Log.e("网络1", path );

                        }
                    }
                });
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {

            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {

            }
        }, mRootView.getRxPermissions(), mErrorHandler);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
